package cn.copera.dyy.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.copera.dyy.common.service.BaseService;
import cn.copera.dyy.dao.TrackDao;
import cn.copera.dyy.entity.Track;

/**
 * 足迹Service
 * 
 */
@Service
@Transactional(readOnly = true)
public class TrackService extends BaseService {

	@Autowired
	private TrackDao trackDao;

	@Transactional(readOnly = false)
	public void save(Track track) {
		trackDao.clear();
		trackDao.save(track);
	}
	@Transactional(readOnly = false)
	public void update(Track track) {
		trackDao.clear();
		trackDao.saveEntity(track);
	}
	public Track get(String id){
		return trackDao.get(id);
	}
	/**
	 * 统计今天留下的足迹
	 * @param account 店家账号(手机号码)
	 * @return
	 */
	public Integer count4Today(String shopId){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		return trackDao.getEntityCount(new Track(), "shop.id = '"+shopId+"' and createDate >= '"+dateStr+" 00:00:00' and createDate <= '"+dateStr+" 23:59:59'");
	}
	/**
	 * 今天的足迹
	 * @param account
	 * @return
	 */
    public List<Track> findTrackToday(String shopId){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		String sql = "shop.id = '"+shopId+"' and createDate >= '"+dateStr+" 00:00:00' and createDate <= '"+dateStr+" 23:59:59'";
		return trackDao.findEntityList(new Track(), "createDate desc", sql);
	}
    /**
     * 最近3天的足迹
     * @param account
     * @return
     */
    public Map<String, Object> findTrackOld(String shopId){
    	Map<String, Object> map = new HashMap<String,Object>();
    	Date date = new Date();
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateSta3 = format.format(new Date(date.getTime() - 3 * 24 * 60 * 60 * 1000));
		String dateSta2 = format.format(new Date(date.getTime() - 2 * 24 * 60 * 60 * 1000));
		String dateSta1 = format.format(new Date(date.getTime() - 1 * 24 * 60 * 60 * 1000));
		String dateEnd = format.format(new Date());
		List<Track> list3 = trackDao.findEntityList(new Track(), "createDate desc", "shop.id = '"+shopId+"' and createDate >= '"+dateSta3+" 00:00:00' and createDate < '"+dateSta2+" 00:00:00'");
		List<Track> list2 = trackDao.findEntityList(new Track(), "createDate desc", "shop.id = '"+shopId+"' and createDate >= '"+dateSta2+" 00:00:00' and createDate < '"+dateSta1+" 00:00:00'");
		List<Track> list1 = trackDao.findEntityList(new Track(), "createDate desc", "shop.id = '"+shopId+"' and createDate >= '"+dateSta1+" 00:00:00' and createDate < '"+dateEnd+" 00:00:00'");
		map.put("list3", list3);
		map.put("list2", list2);
		map.put("list1", list1);
		return map;
	}
    
    //兑换卡点
    public List<Track> findTrack4Exchange(String shopId,String nickName){
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    	List<Track> list = trackDao.find("from Track where shop.id = '"+shopId+"' and nickName = '"+nickName+"' and validDate >= '"+format.format(new Date())+"' and state = '0' order by validDate asc");
    	return list;
    }
    //保存已兑换的卡点状态
    @Transactional(readOnly = false)
    public void changeTrackState(List<Track> list){
    	for (Track track : list) {
			trackDao.saveEntity(track);
			trackDao.flush();
		}
    }
    
    //统计可用卡点
    public Long countPoint(String shopId,String nickName){
    	
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    	
    	return (Long)trackDao.getSession().createQuery("select sum(usableCount) from Track where shop.id = '"+shopId+"' and nickName = '"+nickName+"' and validDate >= '"+format.format(new Date())+"' and state = '0'").uniqueResult();
    }
}
